package com.hue.gulimail.gulimailproduct.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * 自定义线程池
 */
@Configuration
//ThreadPoolProperties上没有@Component注解时可以使用该注解使用配置类
//@EnableConfigurationProperties(ThreadPoolProperties.class)
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolProperties threadPoolProperties) {
        return new ThreadPoolExecutor(threadPoolProperties.getCoreThread(),
                threadPoolProperties.getMaxThread(),
                threadPoolProperties.getKeepAliveTime(),
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100000),
                Executors.defaultThreadFactory(),
                //最大线程和阻塞队列都满时丢弃新来的任务
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
